package com.bobohost.crmrbac.service.impl;


import com.bobohost.crmrbac.dao.mapper.EmployeeMapper;
import com.bobohost.crmrbac.dao.mapper.EmployeeRoleMapper;
import com.bobohost.crmrbac.dao.mapper.RoleMapper;
import com.bobohost.crmrbac.pojo.po.Employee;
import com.bobohost.crmrbac.pojo.query.EmployeeQuery;
import com.bobohost.crmrbac.service.EmployeeService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bobohost.crmrbac.util.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 波波老师
 * @since 2024-12-20
 */
@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements EmployeeService {
    //注入mapper
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private EmployeeRoleMapper employeeRoleMapper;


    @Override
    public PageInfo<Employee> findEmployeeListPage(EmployeeQuery employeeQuery) {
        //----------PageHelper的分页插件---必须放在列表查询的前面
        //参数1：当前页码  参数2：每页最大记录数  ---前端传过来
        PageHelper.startPage(employeeQuery.getCurrentPage(),employeeQuery.getPageSize());
        //查询条件: 最好加上泛型,要查询的表对应的实体类
        QueryWrapper<Employee> qw = new QueryWrapper();
        //关键字
//        if(StringUtils.hasText(employeeQuery.getKeyword())){
//            qw.like("name",employeeQuery.getKeyword());
//        }
/*        //mp的优化写法--条件判断：有值才会拼,name是数据库的列名，不是java的属性字段名！！！！！！
        qw.like(StringUtils.hasText(employeeQuery.getKeyword()),"name",employeeQuery.getKeyword())
                ;
        //如果继续加条件，则默认是and的关系
        qw.or(true).like(StringUtils.hasText(employeeQuery.getKeyword()),"email",employeeQuery.getKeyword());


        //部门id条件
        qw.eq(null != employeeQuery.getDeptId(),"dept_id",employeeQuery.getDeptId());
//        qw.eq(ObjectUtils.isEmpty(employeeQuery.getDeptId()),"dept_id",employeeQuery.getDeptId());*/
        //外层逻辑and,, 代码提示：ctrl+shift+空格
//        qw.and(true,i -> ((QueryWrapper)i).eq(null != employeeQuery.getDeptId(),"dept_id",employeeQuery.getDeptId()))
//                .or(i -> ((QueryWrapper)i).like(StringUtils.hasText(employeeQuery.getKeyword()),"name",employeeQuery.getKeyword())
//                        .or(true).like(StringUtils.hasText(employeeQuery.getKeyword()),"email",employeeQuery.getKeyword())
//                );
        /*qw.and(null != employeeQuery.getDeptId(),i -> i.eq(null != employeeQuery.getDeptId(),"dept_id",employeeQuery.getDeptId()))
        .and(StringUtils.hasText(employeeQuery.getKeyword())
                ,i -> i.like(StringUtils.hasText(employeeQuery.getKeyword()),"name",employeeQuery.getKeyword())
                .or(true).like(StringUtils.hasText(employeeQuery.getKeyword()),"email",employeeQuery.getKeyword())
        );
        */
        /*
        先判断部门id是否存在，如果存在，则拼接部门id的条件
        进行and拼接，and带括号的，使用Lambda表达式来写。
        括号里面是or拼接
         */
        qw.eq(null != employeeQuery.getDeptId(),"dept_id",employeeQuery.getDeptId())
                .and(StringUtils.hasText(employeeQuery.getKeyword())
                        ,i -> i.like("name",employeeQuery.getKeyword())
                        .or()
                        .like("email",employeeQuery.getKeyword())
                );

        //正常查询所有部门列表的
        List<Employee> list = employeeMapper.selectList(qw);

        //封装普通列表为分页列表
        PageInfo<Employee> pageInfo = new PageInfo<>(list);

        return pageInfo;
    }

    @Override
    public void login(String username, String password) {
        QueryWrapper<Employee> qw = new QueryWrapper();
        qw.eq("username",username).eq("password",password);
        Employee employee = employeeMapper.selectOne(qw);
        if(employee == null){
            throw new RuntimeException("用户名或密码错误");
        }
        UserContext.saveEmployee(employee);

    }

    @Override
    public void logout() {
        UserContext.saveEmployee(null);


    }

    @Override
    public Employee findEmployeeByUsername(String username) {

        QueryWrapper<Employee> qw = new QueryWrapper();
        qw.eq("username",username);
        Employee employee = baseMapper.selectOne(qw);
        return employee;
    }


}
